//
//  NSString+ACRegex.m
//  ACFramework
//
//  Created by caoyq on 2018/5/23.
//  Updated by caoyq on 2018/6/22.
//

#import "NSString+ACRegex.h"

@implementation NSString (ACRegex)

- (NSArray *)urlRanges
{
    NSError *error;
    //可以识别url的正则表达式
    NSString *regulaStr = @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";
    
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    NSArray *arrayOfAllMatches = [regex matchesInString:self
                                                options:0
                                                  range:NSMakeRange(0, [self length])];
    
    //NSString *subStr;
    NSMutableArray *arr=[[NSMutableArray alloc] init];
    
    for (NSTextCheckingResult *match in arrayOfAllMatches){
        [arr addObject:[NSValue valueWithRange:match.range]];
    }
    return arr;
}

#pragma mark - 验证

- (BOOL)ac_verifyEmail
{
    NSString *regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    return [self predicateRegex:regex];
}

- (BOOL)ac_verifyPhoneNumber
{
    NSString *regex = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[06-8])\\d{8}$";
    return [self predicateRegex:regex];
}

- (BOOL)ac_verifyPassword
{
    // 6 - 18 位 数字、字母组成
    NSString *regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,18}";
    return [self predicateRegex:regex];
}

- (BOOL)ac_verifyBankCard
{
    NSInteger oddsum = 0;     //奇数求和
    NSInteger evensum = 0;    //偶数求和
    NSInteger allsum = 0;
    NSString *cardNo = self;
    NSInteger cardNoLength = (NSInteger)[cardNo length];
    if (cardNoLength == 0) {
        return NO;
    }
    
    for (NSInteger i = cardNoLength; i >= 1; i--) {
        NSString *tmpString = [cardNo substringWithRange:NSMakeRange(i - 1, 1)];
        NSInteger tmpVal = [tmpString integerValue];
        if (cardNoLength % 2 ==1 ) {
            if((i % 2) == 0){
                tmpVal *= 2;
                if(tmpVal>=10)
                    tmpVal -= 9;
                evensum += tmpVal;
            }else{
                oddsum += tmpVal;
            }
        }else{
            if((i % 2) == 1){
                tmpVal *= 2;
                if(tmpVal>=10)
                    tmpVal -= 9;
                evensum += tmpVal;
            }else{
                oddsum += tmpVal;
            }
        }
    }
    
    allsum = oddsum + evensum;
    if((allsum % 10) == 0)
        return YES;
    else
        return NO;
}

- (BOOL)ac_verifyPlateNumber
{
    NSString *regex = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";
    return [self predicateRegex:regex];
}

- (BOOL)ac_verifyURL
{
    NSString *regex = @"[a-zA-z]+://[^\\s]*";
    return [self predicateRegex:regex];
}

- (BOOL)ac_verifyChinese
{
    NSString *regex = @"[\u4e00-\u9fa5]";
    return [self predicateRegex:regex];
}

- (BOOL)ac_verifyIDCard
{
    NSString *regex = @"^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$";
    return [self predicateRegex:regex];
}

- (BOOL)ac_verifyOnlyDigit
{
    NSString *regex = @"[0-9]*";
    return [self predicateRegex:regex];
}

#pragma mark - private

/** 正则判断 */
- (BOOL)predicateRegex: (NSString *)regex {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [predicate evaluateWithObject:self];
}

@end
